package com.ms.p2p.utils.impl;

import com.ms.common.utils.AppContext;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.entity.loan.Loan;
import com.ms.p2p.entity.loan.LoanStay;
import com.ms.p2p.utils.AverageCapitalPlusInterestUtils;
import com.ms.p2p.utils.LoanStayInfo;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class LoanStayInfoImpl implements LoanStayInfo {


    public LoanStay equalPrincipalAndInterest(Loan borrow, int i,String  ip) {
        LoanStay loanStay = new LoanStay();
        loanStay.setStay_status(0);
        loanStay.setStay_periods(i + 1);
        loanStay.setLoan_id(borrow.getId());
        loanStay.setStay_time(MsDateUtils.getMonthTime(i + 1));

        loanStay.setStay_yesaccount(BigDecimal.valueOf(0L));
        loanStay.setStay_account(AverageCapitalPlusInterestUtils.getPerMonthPrincipalInterest(borrow.getAccount().doubleValue(),borrow.getApr().doubleValue()/100,borrow.getTime_limit()));
        loanStay.setStay_capital(AverageCapitalPlusInterestUtils.getPerMonthPrincipal(borrow.getAccount().doubleValue(),borrow.getApr().doubleValue()/100,borrow.getTime_limit()).get(i+1));
        loanStay.setStay_interest(loanStay.getStay_account().subtract(loanStay.getStay_capital()));
        loanStay.setAddtime(MsDateUtils.getTime());
        loanStay.setAddip(ip);
        return loanStay;


    }


    public LoanStay lumpSumDebt(Loan borrow,String  ip) {
        BigDecimal interest = new BigDecimal(0);
        LoanStay loanStay = new LoanStay();
        loanStay.setStay_status(0);
        loanStay.setStay_periods(1);
        loanStay.setLoan_id(borrow.getId());
        if (borrow.getIs_day() == 1) {
            loanStay.setStay_time(MsDateUtils.getDayTime(borrow.getTime_limit()));
            interest = borrow.getAccount().multiply(borrow.getApr().divide(new BigDecimal(36000), 10, 4))
                    .multiply(BigDecimal.valueOf(borrow.getTime_limit())).setScale(2, 4);
        } else {
            loanStay.setStay_time(MsDateUtils.getMonthTime(borrow.getTime_limit()));
            interest = borrow.getAccount().multiply(borrow.getApr().divide(new BigDecimal(1200), 10, 4))
                    .multiply(BigDecimal.valueOf(borrow.getTime_limit())).setScale(2, 4);
        }

        loanStay.setStay_yesaccount(BigDecimal.valueOf(0L));
        // 一次性还本付息的利息
        loanStay.setStay_account(interest.add(borrow.getAccount()));
        loanStay.setStay_capital(borrow.getAccount());
        loanStay.setStay_interest(interest);
        loanStay.setAddtime(MsDateUtils.getTime());
        loanStay.setAddip(ip);
        return loanStay;
    }


    public LoanStay equalInstallmentPayment(Loan borrow, int order, String stayTime,String ip) {
        BigDecimal interest = borrow.getAccount().multiply(borrow.getApr().divide(new BigDecimal(1200), 10, 4)).setScale(2, 4);// 月利息
        LoanStay loanStay = new LoanStay();
        loanStay.setStay_status(0);

        loanStay.setStay_periods(order + 1);
        loanStay.setLoan_id(borrow.getId());
        loanStay.setStay_time(MsDateUtils.getMonthTime(order + 1));


//        if (order == 0) {//第一期
//            loanStay.setStay_time(MsDateUtils.getDayTimeRepay(order));
//        } else {
//            loanStay.setStay_time(MsDateUtils.getMonthTime(order));
//        }

        loanStay.setStay_time(stayTime);



        loanStay.setStay_capital(borrow.getAccount().divide(new BigDecimal(borrow.getTime_limit()), 10, 4).setScale(2, BigDecimal.ROUND_DOWN));
        BigDecimal interest1 = borrow.getAccount().multiply(borrow.getApr().divide(new BigDecimal(1200), 10, 4))
                .multiply(BigDecimal.valueOf(borrow.getTime_limit())).setScale(2, 4).divide(new BigDecimal(borrow.getTime_limit()), 10, 4).setScale(2, BigDecimal.ROUND_DOWN);
        loanStay.setStay_interest(interest1);
        loanStay.setStay_account(interest.add(loanStay.getStay_capital()).setScale(2, BigDecimal.ROUND_DOWN));
        loanStay.setAddtime(MsDateUtils.getTime());
        loanStay.setAddip(ip);
        return loanStay;
    }


    public LoanStay FirstCouponLaterEdition(Loan borrow, int order,String ip) {
        BigDecimal interest = borrow.getAccount().multiply(borrow.getApr().divide(new BigDecimal(1200), 10, 4)).setScale(2, 4);// 月利息
        LoanStay loanStay = new LoanStay();
        loanStay.setStay_status(0);

        loanStay.setStay_periods(order + 1);
        loanStay.setLoan_id(borrow.getId());

        loanStay.setStay_time(MsDateUtils.getMonthTime(order + 1));

        loanStay.setStay_yesaccount(BigDecimal.valueOf(0L));

        loanStay.setStay_interest(interest);//前面还利息
        if (order == borrow.getTime_limit() - 1) {// 最后一期还本金
            loanStay.setStay_capital(borrow.getAccount());// 本金
            loanStay.setStay_account(interest.add(borrow.getAccount()));// 最后一期还款总额
        } else {
            loanStay.setStay_capital(new BigDecimal(0));
            loanStay.setStay_account(interest);// 最后一期还款总额
        }
        loanStay.setAddtime(MsDateUtils.getTime());
        loanStay.setAddip(ip);
        return  loanStay;

    }
}